using UnityEngine;
using System.Collections;

public class DealsDamage : MonoBehaviour
{
    public int DamageOnCollision = 25;
    public int bossDamageOncollision = 500;
    private Transform Wave = null;
    private Manager difficultyManager = null;
        private EnemyDifficultyManager enemyDifficulty;

    //need more explosions;
    public void Start()
    {
        if (gameObject.tag == "PlayerBullet")
        {
            DamageOnCollision += PlayerPrefs.GetInt("DamageBoost");
        }
        if (gameObject.tag == "AttackBullet")
        {
            DamageOnCollision += PlayerPrefs.GetInt("AttackBoost");
        }
        if (gameObject.GetComponent<AIBase>() != null)
        {
            Wave = GameObject.FindGameObjectWithTag("WaveManager").transform;
            foreach (Transform child in Wave)
            {
                if (child.gameObject.activeInHierarchy)
                {
                    enemyDifficulty = child.gameObject.GetComponent<EnemyDifficultyManager>();
                }
            }
            enemyDifficulty.Add(DamageOnCollision, enemyDifficulty.CollisionDamageDealt.Adder, out DamageOnCollision);
            enemyDifficulty.AddPercentage(DamageOnCollision, enemyDifficulty.CollisionDamageDealt.PercentageMultiplier, out DamageOnCollision);
            if (enemyDifficulty.CollisionDamageDealt.EqualToEnabled)
            {
                DamageOnCollision = (int)Mathf.Floor(enemyDifficulty.CollisionDamageDealt.EqualTo);
            }
        }
    }
    public void OnCollisionEnter(Collision col)
    {
		
//        Debug.Log("col: " + col.gameObject.name);
        CanTakeDamage toHurt;
        if ((toHurt = col.gameObject.GetComponent<CanTakeDamage>()) != null)
        {
            //If not trigger put here
			
			CanTakeDamage howToDie; 
			if(GetComponent<AIBase>() != null) 
			{
				howToDie = GetComponent<AIBase>();
			}
			else if(GetComponent<orbit>() != null)
			{
				howToDie = GetComponent<orbit>();
			}
			else
			{
				 howToDie = GetComponent<CanTakeDamage>();
			}
            //Debug.Log("toHurt: "+toHurt.name);

            
            if (howToDie)
            {
                //Debug.Log("HowToDie: " + howToDie.name);
                if (!howToDie.tag.StartsWith("Boss"))
                {

                    toHurt.TakeDamage(DamageOnCollision);
                    howToDie.Die();
                }
                else
                    toHurt.TakeDamage(bossDamageOncollision);
            }
            else
            {
                //Debug.Log(toHurt.name + "damage " + DamageOnCollision);
                toHurt.TakeDamage(DamageOnCollision);
                //Debug.Log("Destroy "+gameObject.name);
				if(gameObject.tag == "Shockwave")
				{
					gameObject.collider.isTrigger = true;
//					(gameObject.collider as SphereCollider).radius = 3;
                	return;
				}
				Destroy(gameObject);
            }
        }

    }
    /* 
	  
      BlackHole Detection
	 
    */
    public void OnTriggerEnter(Collider col)
    {
        CanTakeDamage toHurt;
        Vortex vortex;
        orbit orbits;
		if(gameObject.tag == "Boss3" && col.collider.gameObject.tag == "Player")
		{
			Handheld.Vibrate();
			col.gameObject.GetComponent<CanTakeDamage>().TakeDamage(bossDamageOncollision);
		}
        if ((orbits = col.gameObject.GetComponent<orbit>()) != null)
        {
//			if(orbits.gameObject.tag != "HodorSpawn")
//				Spawner.waveEnemies--;
            	Destroy(transform.parent.gameObject);
            //orbits.enabled = false;
        }
        if ((toHurt = col.gameObject.GetComponent<CanTakeDamage>()) != null)
        {
            if (gameObject.tag == "PlayerBlackHole")
            {
                if (col.gameObject.GetComponent<Vortex>() == null)
                {
                    vortex = col.gameObject.AddComponent<Vortex>();
                    vortex.vortexLocation = transform.position;
                    vortex.gravataionalSpeed = toHurt.gravataionalSpeed;
                    vortex.blackHole = gameObject;
                }

            }
        }
    }
}